package com.elvin.zoe.parser;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import com.elvin.zoe.pojo.Conversation;
import com.elvin.zoe.pojo.Message;

public abstract class AbstractParser implements Parser {

    public String readFile(File file) throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        //        BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
        while (true) {
            String str = bufferedReader.readLine();
            if (str == null)
                break;
            stringBuffer.append(str.trim() + "\n");
        }
        return stringBuffer.toString();
    }


    @Override
    public Conversation parseMessages(String originalDoc) throws Exception {
        String with = getMessagingWith(originalDoc);
        List<Message> messages = parseMessagesFromTokens(getMessageTokens(originalDoc));
        return new Conversation(with, messages);
    }

    protected abstract List<Message> parseMessagesFromTokens(List<String> messageTokens) throws Exception;

    protected String getMessagingWith(String originalHtml){
        return "沈佳萍";
    }

    protected abstract List<String> getMessageTokens(String originalHtml);

    protected abstract int getMessageType(String messageToken);

    protected abstract String getMessageDatetime(String messageToken);

    protected abstract String getMessageDatetimeFormat();

}
